<template>
	<view class="container">
		<view class="example">
			<uni-forms ref="form" :model="xfzhl" label-width="100px" >
				<!-- B超 -->
				<uni-section title="B超" type="line"></uni-section>
				<uni-forms-item label="病灶（cm）" name="bcBzCd">
					<uni-row class="demo-uni-row">
						<uni-col :span="6">
							<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.bcBzCd" type="digit"  @change="autoSaven" placeholder="请输入大小（cm）" />
						</uni-col>
						<uni-col :span="3" class="wbkx">x</uni-col>
						<uni-col :span="6">
							<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.bcBzKd" type="digit"  @change="autoSaven" placeholder="请输入大小（cm）" />
						</uni-col>
						<uni-col :span="3" class="wbkx">x</uni-col>
						<uni-col :span="6">
							<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.bcBzGd" type="digit"  @change="autoSaven" placeholder="请输入大小（cm）" />
						</uni-col>
					</uni-row>
				</uni-forms-item>
				<uni-forms-item label="淋巴结（cm）" name="bcLbjCd">
					<uni-row class="demo-uni-row">
						<uni-col :span="11">
							<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.bcLbjCd" type="digit"  @change="autoSaven" placeholder="请输入大小（cm）" />
						</uni-col>
						<uni-col :span="2" class="wbkx">x</uni-col>
						<uni-col :span="11">
							<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.bcLbjKd" type="digit"  @change="autoSaven" placeholder="请输入大小（cm）" />
						</uni-col>
					</uni-row>
				</uni-forms-item>
				
				<!-- 钼靶 -->
				<uni-section title="钼靶" type="line"></uni-section>
				<uni-forms-item label="病灶（cm）" name="mbBzCd">
					<uni-row class="demo-uni-row">
						<uni-col :span="11">
							<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.mbBzCd" type="digit"  @change="autoSaven" placeholder="请输入大小（cm）" />
						</uni-col>
						<uni-col :span="2" class="wbkx">x</uni-col>
						<uni-col :span="11">
							<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.mbBzKd" type="digit"  @change="autoSaven" placeholder="请输入大小（cm）" />
						</uni-col>
					</uni-row>
				</uni-forms-item>
				<uni-forms-item label="淋巴结（cm）" name="mbLbjCd">
					<uni-row class="demo-uni-row">
						<uni-col :span="11">
							<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.mbLbjCd" type="digit"  @change="autoSaven" placeholder="请输入大小（cm）" />
						</uni-col>
						<uni-col :span="2" class="wbkx">x</uni-col>
						<uni-col :span="11">
							<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.mbLbjKd" type="digit"  @change="autoSaven" placeholder="请输入大小（cm）" />
						</uni-col>
					</uni-row>
				</uni-forms-item>
				
				<!-- 磁共振 -->
				<uni-section title="磁共振" type="line"></uni-section>
				<uni-forms-item label="病灶（cm）" name="cgzBzCd">
					<uni-row class="demo-uni-row">
						<uni-col :span="6">
							<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.cgzBzCd" type="digit"  @change="autoSaven" placeholder="请输入大小（cm）" />
						</uni-col>
						<uni-col :span="3" class="wbkx">x</uni-col>
						<uni-col :span="6">
							<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.cgzBzKd" type="digit"  @change="autoSaven" placeholder="请输入大小（cm）" />
						</uni-col>
						<uni-col :span="3" class="wbkx">x</uni-col>
						<uni-col :span="6">
							<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.cgzBzGd" type="digit"  @change="autoSaven" placeholder="请输入大小（cm）" />
						</uni-col>
					</uni-row>
				</uni-forms-item>
				<uni-forms-item label="淋巴结（cm）" name="cgzLbjCd">
					<uni-row class="demo-uni-row">
						<uni-col :span="11">
							<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.cgzLbjCd" type="digit"  @change="autoSaven" placeholder="请输入大小（cm）" />
						</uni-col>
						<uni-col :span="2" class="wbkx">x</uni-col>
						<uni-col :span="11">
							<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.cgzLbjKd" type="digit"  @change="autoSaven" placeholder="请输入大小（cm）" />
						</uni-col>
					</uni-row>
				</uni-forms-item>
				<uni-forms-item label="ADC值" name="cgzAdc">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.cgzAdc"  @change="autoSaven" placeholder="请输入ADC" />
				</uni-forms-item>
				
				<!-- 血常规 -->
				<uni-section title="血常规" type="line"></uni-section>
				<uni-forms-item label="白细胞（10^9/L）" name="xcgBxb">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.xcgBxb"  @change="autoSaven" placeholder="请输入白细胞" />
				</uni-forms-item>
				<uni-forms-item label="中性粒细胞（10^9/L）" name="xcgZxlxb">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.xcgZxlxb"  @change="autoSaven" placeholder="请输入中性粒细胞" />
				</uni-forms-item>
				<uni-forms-item label="红细胞（10^12/L）" name="xcgHxb">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.xcgHxb"  @change="autoSaven" placeholder="请输入红细胞" />
				</uni-forms-item>
				<uni-forms-item label="血小板（10^9/L）" name="xcgXxb">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.xcgXxb"  @change="autoSaven" placeholder="请输入血小板" />
				</uni-forms-item>
				
				<!-- 肿瘤标志物 -->
				<uni-section title="肿瘤标志物" type="line"></uni-section>
				<uni-forms-item label="CEA（ng/mL）" name="zlbzwGea">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.zlbzwGea"  @change="autoSaven" placeholder="请输入CEA" />
				</uni-forms-item>
				<uni-forms-item label="CA199（U/mL）" name="zlbzwCa199">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.zlbzwCa199"  @change="autoSaven" placeholder="请输入CA199" />
				</uni-forms-item>
				<uni-forms-item label="CA15-3（U/mL）" name="zlbzwCa153">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.zlbzwCa153"  @change="autoSaven" placeholder="请输入CA15-3" />
				</uni-forms-item>
				<uni-forms-item label="CA125（U/mL）" name="zlbzwCa125">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.zlbzwCa125"  @change="autoSaven" placeholder="请输入CA125" />
				</uni-forms-item>
				
				<!-- 生化 -->
				<uni-section title="生化" type="line"></uni-section>
				<uni-forms-item label="肌酐（μmoI/L）" name="shJg">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.shJg"  @change="autoSaven" placeholder="请输入肌酐" />
				</uni-forms-item>
				<uni-forms-item label="ALT（U）" name="shAlt">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.shAlt"  @change="autoSaven" placeholder="请输入ALT" />
				</uni-forms-item>
				<uni-forms-item label="AST（U）" name="shAst">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.shAst"  @change="autoSaven" placeholder="请输入AST" />
				</uni-forms-item>
				<uni-forms-item label="碱性磷酸酶（U/L）" name="shJxlsm">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.shJxlsm"  @change="autoSaven" placeholder="请输入碱性磷酸酶" />
				</uni-forms-item>
				
				<!-- 电解质 -->
				<uni-section title="电解质" type="line"></uni-section>
				<uni-forms-item label="钠（mmol/L）" name="djzNa">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.djzNa"  @change="autoSaven" placeholder="请输入钠" />
				</uni-forms-item>
				<uni-forms-item label="钾（mmol/L）" name="djzJia">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.djzJia"  @change="autoSaven" placeholder="请输入钾" />
				</uni-forms-item>
				<uni-forms-item label="镁（mmol/L）" name="djzMei">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.djzMei"  @change="autoSaven" placeholder="请输入镁" />
				</uni-forms-item>
				<uni-forms-item label="钙（mmol/L）" name="djzGai">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.djzGai"  @change="autoSaven" placeholder="请输入钙" />
				</uni-forms-item>
				
				<!-- 激素 -->
				<uni-section title="激素" type="line"></uni-section>
				<uni-forms-item label="孕酮（nmo1/L）" name="jsYt">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.jsYt"  @change="autoSaven" placeholder="请输入孕酮" />
				</uni-forms-item>
				<uni-forms-item label="雌二醇（pmo1/L）" name="jsXec">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.jsXec"  @change="autoSaven" placeholder="请输入雌二醇" />
				</uni-forms-item>
				<uni-forms-item label="卵泡刺激素（IU/L）" name="jsNbcjs">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.jsNbcjs"  @change="autoSaven" placeholder="请输入卵泡刺激素" />
				</uni-forms-item>
				<uni-forms-item label="促黄体生成素（IU/L）" name="jsChtscs">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.jsChtscs"  @change="autoSaven" placeholder="请输入促黄体生成素" />
				</uni-forms-item>
				<uni-forms-item label="泌乳素（μg/L）" name="jsMrs">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.jsMrs"  @change="autoSaven" placeholder="请输入泌乳素" />
				</uni-forms-item>
				<uni-forms-item label="睾酮（nmo1/L）" name="jsGt">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.jsGt"  @change="autoSaven" placeholder="请输入睾酮" />
				</uni-forms-item>
				
				<!-- 其他 -->
				<uni-section title="其他" type="line"></uni-section>
				<uni-forms-item label="化疗方案" name="hlfa">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.hlfa"  @change="autoSaven" placeholder="请输入化疗方案" />
				</uni-forms-item>
				<uni-forms-item label="评估时间" name="pgsj">
					<uni-datetime-picker type="date" :clear-icon="true" v-model="xfzhl.pgsj" />
				</uni-forms-item>
				<uni-forms-item label="标题" name="pgbt">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.pgbt"  @change="autoSaven" placeholder="请输入标题" />
				</uni-forms-item>
				<uni-forms-item label="疗效评估" name="lspg">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.lspg"  @change="autoSaven" placeholder="请输入疗效评估" />
				</uni-forms-item>
				<uni-forms-item label="是否升白" name="sfsb">
					<uni-data-checkbox :disabled="isDisabled" v-model="xfzhl.sfsb" @change="ycxsChange($event,'sfsbCxdxShow','sfsbCxdx')" :localdata="dictSf" />
				</uni-forms-item>
				<uni-forms-item label="升白药物" v-show="sfsbCxdxShow" name="sfsbCxdx">
					<uni-data-select :disabled="isDisabled"
					      v-model="xfzhl.sfsbCxdx"
					      :localdata="sfsbCxdxList"></uni-data-select>
				</uni-forms-item>
				<uni-forms-item label="更换方案" name="ghfa">
					<uni-data-checkbox :disabled="isDisabled" v-model="xfzhl.ghfa" @change="ycxsChange($event,'newFaShow','newFa')" :localdata="dictSf" />
				</uni-forms-item>
				<uni-forms-item v-show="newFaShow" label="方案名称" name="newFa">
					<uni-easyinput  :disabled="isDisabled" v-model="xfzhl.newFa"  @change="autoSaven" placeholder="请输入方案名称" />
				</uni-forms-item>
				<uni-forms-item label="备注" name="remark">
					<uni-easyinput  :disabled="isDisabled" type="textarea" autoHeight v-model="xfzhl.remark"  @change="autoSaven" placeholder="请输入备注"/>
				</uni-forms-item>
				<button type="primary" :disabled="isDisabled" @click="submit">保存</button>
			</uni-forms>
		</view>
	</view>
</template>

<script>
	import storage from '@/utils/storage'
import { addXfzzl,updateXfzzl,getDicts,sfkybjData } from "@/api/trms/trms"
import { getPgInfo } from "@/api/trms/query"
export default {
	data() {
		return {
			id: null,
			isDisabled: false,
			userId: null,
			sfsbCxdxShow: false,
			size: 1,
			newFaShow: false,
			sfsbCxdxList: [],
			// 表单数据
			xfzhl: {
				pglx: 2
			},
			rules: {
			},
			dictSf: [
				{
				  text: '是',
				  value: 1
				}, {
				  text: '否',
				  value: 0
				}
			]
		}
	},
	onLoad: function(option) {
		this.getDictSbxg();
		this.xfzhl.patientId = option.patientId;
		this.id = option.id;
		this.size = option.size;
		this.userId = option.userId;
		let that = this;
		console.log(".....edit.....",this.id,this.patientId)
		this.eventChannel = this.getOpenerEventChannel();
		// 监听listFzhl事件，获取上一页面通过eventChannel传送到当前页面的数据
		this.eventChannel.on('listFzhl', function(data) {
			
		});
		sfkybjData(this);
		if(this.id){
			// 查询详情
			this.getDataById();
		}else{
			this.xfzhl.pgbt = "第"+this.size+"次评估";
		}
	},
	onReady() {
		this.$refs.form.setRules(this.rules)
	},
	methods: {
		/**
		 * 获取升白效果
		 */
		getDictSbxg(){
			getDicts("trms_sbxg").then(r => {
				this.sfsbCxdxList = this.dictFormat(r.data);
			});
		},
		/**
		 * 获取数据
		 */
		getDataById(){
			getPgInfo(this.id).then(response => {
			  this.xfzhl = response.data;
			  this.xfzhl.sfsbCxdx = this.xfzhl.sfsbCxdx + '';
			  this.initShow(response.data);
			});
		},
		/**
		 * 修改
		 */
		submit(){
			this.$refs.form.validate().then(res => {
				if(this.id){
					// 编辑
					updateXfzzl(this.xfzhl).then(response => {
					  if(response.code == 200){
						  this.$modal.msgSuccess("修改成功");
						  this.backWork();
						  return;
					  }
					  this.$modal.msgError(response.msg);
					});
				}else{
					// 新增
					this.xfzhl.pglx = 2;
					addXfzzl(this.xfzhl).then(response => {
					  if(response.code == 200){
						  this.$modal.msgSuccess("新增成功");
						  this.backWork();
						  return;
					  }
					  this.$modal.msgError(response.msg);
					});
				}
			})
		},
		/**
		 * 自动修改
		 */
		autoSaven(){
			let that = this;
			this.$refs.form.validate().then(res => {
				if(this.id){
					// 编辑
					updateXfzzl(this.xfzhl).then(response => {
					  console.log("修改成功")
					});
				}else{
					// 新增
					this.xfzhl.pglx = 2;
					addXfzzl(this.xfzhl).then(response => {
						if(response.code == 200){
							that.id = response.data.id;
							that.xfzhl.id = response.data.id;
						}
					  console.log("新增成功")
					});
				}
			})
		},
		/**
		 * 回到工作页面
		 */
		backWork(){
			let that = this;
			setTimeout(function (){
				// 向起始页通过事件传递数据
				that.eventChannel.emit('listFzhl', {data: 'refPage'});
				uni.navigateBack({
					delta: 1
				});
			},500);
		},
		ycxsChange(e,show,valId){
			const val = e.detail.value;
			this[show] = val == 1;
			if(Number(val) == 0 ){
				// 清理值
				this.xfzhl[valId] = null;
			}
		},
		/**
		 * 隐藏或者显示
		 * @param {Object} jbxx 
		 */
		showAndHide(jbxx){
			this.ssfsShow = jbxx.sfss == 1;
		},
		/**
		 * 给字典格式化成uniapp插件需要的格式
		 * @param {Object} list
		 */
		dictFormat(list){
			let newArr = [];
			if(!list){
				return newArr;
			}
			list.forEach(d=>{
				newArr.push({value: d.dictValue,text: d.dictLabel})
			})
			return newArr;
		},
		/**
		 * 初始化隐藏显示
		 */
		initShow(lctz){
			this.sfsbCxdxShow = lctz.sfsb == 1;
			this.newFaShow = lctz.ghfa == 1;
		}
	}
}
</script>


<style lang="scss">
  page {
    background-color: #ffffff;
  }

  .example {
    padding: 15px;
    background-color: #fff;
  }

  .segmented-control {
    margin-bottom: 15px;
  }

  .button-group {
    margin-top: 15px;
    display: flex;
    justify-content: space-around;
  }

  .form-item {
    display: flex;
    align-items: center;
    flex: 1;
  }

  .button {
    display: flex;
    align-items: center;
    height: 35px;
    line-height: 35px;
    margin-left: 10px;
  }
  .wbkx{
  	  text-align: center;
  	  margin-top: 10px;
  }
</style>
